package com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.comment;


import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.util.ExportUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


/**
 * 表头的批注处理器
 * */
@Data
@AllArgsConstructor
public class HeadCommentRowWriteHandler<T> implements RowWriteHandler {

    private final Class<T> headerClass;

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if(isHead){
            Map<String, HeadCommentResolve> commentMap = getAnnotations(headerClass);
            Map<String,Integer>  cellPositionMap = ExportUtil.cellPosition( row );
            // 这里可以对cell进行任何操作
            Sheet sheet = writeSheetHolder.getSheet();
            commentMap.forEach((headName, resolve) -> {
                Integer index = cellPositionMap.get(headName) ;
                if(null!=index){
                    Cell cell = row.getCell(index);

                    ClientAnchor anchor = new XSSFClientAnchor();
                    // 关键修改
                    // anchor.setDx1(0);
                    // anchor.setDx2(0);
                    // anchor.setDy1(0);
                    // anchor.setDy2(0);
                    anchor.setCol1(cell.getColumnIndex());
                    anchor.setRow1(cell.getRowIndex());
                    // anchor.setCol2(cell.getColumnIndex() + 5);
                    // anchor.setRow2(cell.getRowIndex() + 6);
                    anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);

                    // 结束
                    Drawing drawing = sheet.createDrawingPatriarch();
                    Comment comment = drawing.createCellComment(anchor);
                    // 输入批注信息
                    comment.setString(new XSSFRichTextString(resolve.getValue()));
                    // 将批注添加到单元格对象中
                    cell.setCellComment(comment);
                }
            });
        }
    }

    /**
     *
     * @param headerClass 表头类
     * @param <T> 泛型
     * @return map
     */
    private <T> Map<String, HeadCommentResolve> getAnnotations(Class<T> headerClass) {
        Map<String, HeadCommentResolve> selectedMap = new HashMap<>();

        // getDeclaredFields(): 返回全部声明的属性；getFields(): 返回public类型的属性
        Field[] fields = headerClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++){
            Field field = fields[i];
            // 解析注解信息
            HeadComment comment = field.getAnnotation(HeadComment.class);
            ExcelProperty property = field.getAnnotation(ExcelProperty.class);
            if (comment != null && property != null ) {
                HeadCommentResolve resolve = new HeadCommentResolve();
                resolve.setValue(comment.value());
                // resolve.setWidth(comment.width());
                // resolve.setHeigh(comment.heigh());

                selectedMap.put(property.value()[0], resolve);
            }
        }
        return selectedMap;
    }

}